package org.mullin.stratego.client;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import org.mullin.stratego.shared.Color;
import org.mullin.stratego.shared.Coord;
import org.mullin.stratego.shared.Move;
import org.mullin.stratego.shared.Rank;
import org.mullin.stratego.shared.State;


public class BadPlayer {

	private int color;
	private Random r = new Random();
	//private List<Coord> moveablePieces;
	private State s;
	
	public BadPlayer(int color, State s) {
		this.color = color;
		this.s = s;
//		moveablePieces = new ArrayList<Coord>();
//		
//		int i;
//		i = (color == Color.BLUE) ? 6 : 3;
//		for (int j = 0; j < 9; j++) {
//			Coord c = new Coord(i, j);
//			if (!s.getPiece(c).rank.equals(Rank.BOMB) && !s.getPiece(c).rank.equals(Rank.FLAG)) {
//				moveablePieces.add(new Coord(i, j));
//			}
//		}
	}
	
	private boolean isValidCoord(Coord c) {
		return c.x >= 0 && c.x < State.COLS 
				&& c.y >= 0 && c.y < State.ROWS;
	}
	
	public Map<Coord, List<Coord>> findFreePieces() {
		Map<Coord, List<Coord>> ret = new HashMap<Coord, List<Coord>>();
		
		for (int i = 0; i < 10; i++) {
			for (int j = 0; j < 10; j++) {
				Coord c = new Coord(i, j);
				System.out.println("findFreePieces- at coord " + c + " color of piece=" + Color.toString(s.getPiece(c).color));
				if (s.getPiece(c).color == color) {
					System.out.println("findFreePieces- checking open spots for piece at coord " + c);
					List<Coord> possibleMoves = new ArrayList<Coord>();
					
					Coord d = new Coord(i+1, j);
					Coord r = new Coord(i, j+1);
					Coord l = new Coord(i, j-1);
					Coord u = new Coord(i-1, j);
					
					if ( isValidCoord(d) && (s.getPiece(d).equals(State.LAND) || s.getPiece(d).color != color) && !s.getPiece(d).equals(State.WATER)) {
						System.out.println("spot at " + d + " is available, adding to possible moves.");
						possibleMoves.add(d);
					}
					if ( isValidCoord(r) && (s.getPiece(r).equals(State.LAND) || s.getPiece(r).color != color) && !s.getPiece(r).equals(State.WATER)) {
						System.out.println("spot at " + r + " is available, adding to possible moves.");
						possibleMoves.add(r);
					}
					if ( isValidCoord(l) && (s.getPiece(l).equals(State.LAND) || s.getPiece(l).color != color) && !s.getPiece(l).equals(State.WATER)) {
						System.out.println("spot at " + l + " is available, adding to possible moves.");
						possibleMoves.add(l);
					}
					if ( isValidCoord(u) && (s.getPiece(u).equals(State.LAND) || s.getPiece(u).color != color) && !s.getPiece(u).equals(State.WATER)) {
						System.out.println("spot at " + u + " is available, adding to possible moves.");
						possibleMoves.add(u);
					}
					
					if (!possibleMoves.isEmpty()) {
						ret.put(c, possibleMoves);
					}
				}
			}
		}
		return ret;
	}
	
	public Move findMove(State s) {
		
		Map<Coord, List<Coord>> possibles = findFreePieces();
		System.out.println("possibles=" + possibles);
		int ida = r.nextInt(possibles.size());
		Coord start = (Coord) possibles.keySet().toArray()[ida];
		List<Coord> targets = possibles.get(start);
		int idb = r.nextInt(targets.size());
		Coord next = targets.get(idb);
		
		return new Move(next, start);
	}
	
}
